package com.java.business.system.permis.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.java.business.system.permis.bean.Role;
import com.java.business.system.permis.bean.RoleMenu;
import com.java.business.system.permis.dao.RoleMapper;
import com.java.core.utils.IdUtils;

@Transactional
@Service
public class RoleService {

	@Autowired
	private RoleMapper mapper;

	public void add(Role role) throws RuntimeException {
		role.setRoleid(IdUtils.uuid());
		mapper.add(role);
	}

	public void update(Role role) throws RuntimeException {
		mapper.update(role);
	}

	public void setDisabled(Role role) throws RuntimeException {
		mapper.setDisabled(role);
	}

	public void setMenus(String roleid, String[] menuids) {
		if (StringUtils.isBlank(roleid) || roleid.equals("0")) {
			return;
		}
		mapper.clearMenus(roleid);
		for (String menuid : menuids) {
			RoleMenu rm = new RoleMenu();
			rm.setRoleid(roleid);
			rm.setMenuid(menuid);
			mapper.setMenus(rm);
		}
	}

	public void delete(Role role) throws RuntimeException {
		mapper.delete(role.getRoleid());
	}

	public Role getRoleById(Role role) {
		return mapper.getRoleById(role.getRoleid());
	}

	public List<Role> getRoleList(Role role) {
		return mapper.getRoleList(role);
	}

	public List<Role> getRoleListByUser(String userid) {
		return mapper.getRoleListByUser(userid);
	}

	public List<Role> getRoleListNotUser(String userid) {
		return mapper.getRoleListNotUser(userid);
	}
}
